package com.tang.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.tang.dto.BookDTO;
import com.tang.dto.UserDTO;
import com.tang.exception.BusinessException;
import com.tang.exception.BusinessExceptionCode;
import com.tang.util.DeepBeanUtils;
import com.tang.vo.Book;
import com.tang.vo.User;
import com.tang.service.UserService;
import com.tang.mapper.UserMapper;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.DigestUtils;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
* @author tang
* @description 针对表【user(用户)】的数据库操作Service实现
* @createDate 2023-05-01 18:19:48
*/
@Service
public class UserServiceImpl  implements UserService{

    @Autowired
    private UserMapper userMapper;

    @Override
    public UserDTO login(UserDTO userDTO) {
        if (ObjectUtils.isNotEmpty(userDTO.getLoginName())){
            QueryWrapper<User> wrapper = new QueryWrapper<>();
            wrapper.eq("login_name",userDTO.getLoginName());
            User user = userMapper.selectOne(wrapper);
            if (ObjectUtils.isEmpty(user)){
                throw new BusinessException(BusinessExceptionCode.LOGIN_USER_ERROR);
            }else {
                if (user.getPassword().equals(DigestUtils.md5DigestAsHex(userDTO.getPassword().getBytes()))){
                    UserDTO dto = new UserDTO();
                    //登录成功
                    DeepBeanUtils.copyProperties(user, dto);
                    return dto;
                }else {
                    throw new BusinessException(BusinessExceptionCode.LOGIN_USER_ERROR);
                }
            }
        }
        return null;
    }

    @Override
    public List<UserDTO> all() {
        QueryWrapper<User> wrapper=new QueryWrapper<>();
        List<User> users = userMapper.selectList(wrapper);
        return DeepBeanUtils.copyListProperties(users, UserDTO::new);
    }

    @Override
    public Map<String, Object> list(String loginName, int pageNum, int pageSize) {
        QueryWrapper<User> wrapper=new QueryWrapper<>();
        Map<String,Object> map=new HashMap<>();
        if (ObjectUtils.isNotEmpty(loginName)){
            wrapper.like("name",loginName);
        }
        PageHelper.startPage(pageNum,pageSize);
        List<User> users = userMapper.selectList(wrapper);
        PageInfo<User> pageInfo=new PageInfo<>(users);
        List<UserDTO> userDTOS = DeepBeanUtils.copyListProperties(users, UserDTO::new);
        map.put("data",userDTOS);
        map.put("total",pageInfo.getTotal());
        map.put("pages",pageInfo.getPages());
        return map;
    }

    @Override
    public int update(UserDTO userDTO) {
        User user = new User();
        BeanUtils.copyProperties(userDTO,user);
        if (ObjectUtils.isNotEmpty(userDTO.getId())&&!userDTO.getId().equals("0")&&ObjectUtils.isNotEmpty(userDTO.getLoginName())&&ObjectUtils.isNotEmpty(userDTO.getPassword())){
            QueryWrapper<User> wrapper = new QueryWrapper<>();
            wrapper.eq("id",user.getId()).eq("login_name",user.getLoginName());
            return userMapper.update(user,wrapper);
        }
        if (ObjectUtils.isNotEmpty(userDTO.getLoginName())){
            QueryWrapper<User> wrapper = new QueryWrapper<>();
            wrapper.eq("login_name",user.getLoginName());
            User user1 = userMapper.selectOne(wrapper);
            if (ObjectUtils.isEmpty(user1)){
                user.setId(null);
                user.setPassword(DigestUtils.md5DigestAsHex(user.getPassword().getBytes()));
                return userMapper.insert(user);
            }else {
                throw new BusinessException(BusinessExceptionCode.USER_LOGIN_NAME_EXIST);
            }
        }
        return 0;
    }
    @Override
    public int updatePassword(UserDTO userDTO) {
        User user = new User();
        BeanUtils.copyProperties(userDTO,user);
        if (ObjectUtils.isNotEmpty(userDTO.getId())&&!userDTO.getId().equals("0")&&ObjectUtils.isNotEmpty(userDTO.getLoginName())&&ObjectUtils.isNotEmpty(userDTO.getPassword())){
            QueryWrapper<User> wrapper = new QueryWrapper<>();
            wrapper.eq("id",user.getId()).eq("login_name",user.getLoginName());
            System.out.println(user.getPassword());
            if ("".equals(user.getPassword().trim())){
                throw new BusinessException(BusinessExceptionCode.USER_PASSWORD_NOT_NULL);
            }
            user.setPassword(DigestUtils.md5DigestAsHex(user.getPassword().getBytes()));
            return userMapper.update(user,wrapper);
        }
        return 0;
    }

    @Override
    public int delete(String id) {
        if (ObjectUtils.isNotEmpty(id)){
            return userMapper.deleteById(id);
        }
        return 0;
    }
}




